﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.MultiEditingInPathEditor.SetLocationsProperties.CApSLPSetLocationsPropertiesResetMechanism
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System.Collections.Generic;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.MultiEditingInPathEditor.SetLocationsProperties;

internal class CApSLPSetLocationsPropertiesResetMechanism
{
  private Dictionary<LocationPropertyIdKey, object> _oldValues;
  private Dictionary<LocationPropertyIdKey, object> _newValues;
  private List<string> _propertyIds;
  private const string _CUSTOMIZED_MOTION = "Customized Motion";
  private const string _CONFIG = "Config";
  private const string _EXTERNAL_AXES_VALUES = "General_External Axes Values";

  public CApSLPSetLocationsPropertiesResetMechanism()
  {
    this._oldValues = new Dictionary<LocationPropertyIdKey, object>();
    this._newValues = new Dictionary<LocationPropertyIdKey, object>();
    this._propertyIds = new List<string>();
  }

  public void SetValue(
    ITxRoboticLocationOperation location,
    string propertyId,
    object oldValue,
    object newValue)
  {
    if (!this.AreListsValid())
      return;
    LocationPropertyIdKey relevantKey1 = this.GetRelevantKey(this._newValues, location, propertyId);
    LocationPropertyIdKey relevantKey2 = this.GetRelevantKey(this._oldValues, location, propertyId);
    if (relevantKey1 != null && relevantKey2 != null)
    {
      this._newValues[relevantKey1] = newValue;
    }
    else
    {
      LocationPropertyIdKey key = new LocationPropertyIdKey(location, propertyId);
      this._oldValues.Add(key, oldValue);
      this._newValues.Add(key, newValue);
      if (this._propertyIds.Contains(propertyId))
        return;
      this._propertyIds.Add(propertyId);
    }
  }

  public void SetOldValue(ITxRoboticLocationOperation location, string propertyId, object oldValue)
  {
    if (propertyId.Contains("General_External Axes Values"))
      oldValue = (object) location.RobotExternalAxesData;
    else if (propertyId.Contains("Config"))
      oldValue = (object) location.RobotConfigurationData;
    this.SetValue(location, propertyId, oldValue, (object) null);
  }

  public void SetNewValue(ITxRoboticLocationOperation location, string propertyId, object newValue)
  {
    if (propertyId.Contains("General_External Axes Values") && !(newValue is TxRobotExternalAxisData[]))
      newValue = (object) null;
    else if (propertyId.Contains("Config") && !(newValue is TxRobotConfigurationData))
      newValue = (object) null;
    this.SetValue(location, propertyId, (object) null, newValue);
  }

  public Dictionary<string, object> GetAllOriginalValues(ITxRoboticLocationOperation location)
  {
    Dictionary<string, object> allOriginalValues = (Dictionary<string, object>) null;
    if (this.AreListsValid())
    {
      allOriginalValues = new Dictionary<string, object>();
      foreach (KeyValuePair<LocationPropertyIdKey, object> oldValue in this._oldValues)
      {
        foreach (string propertyId in this._propertyIds)
        {
          LocationPropertyIdKey relevantKey = this.GetRelevantKey(this._oldValues, location, propertyId);
          if (relevantKey != null && oldValue.Key.Equals(relevantKey))
          {
            allOriginalValues.Add(propertyId, oldValue.Value);
            break;
          }
        }
      }
    }
    return allOriginalValues;
  }

  public Dictionary<string, object> GetAllCurrentValues(ITxRoboticLocationOperation location)
  {
    Dictionary<string, object> allCurrentValues = (Dictionary<string, object>) null;
    if (this.AreListsValid())
    {
      allCurrentValues = new Dictionary<string, object>();
      foreach (KeyValuePair<LocationPropertyIdKey, object> newValue in this._newValues)
      {
        foreach (string propertyId in this._propertyIds)
        {
          LocationPropertyIdKey relevantKey = this.GetRelevantKey(this._newValues, location, propertyId);
          if (relevantKey != null && newValue.Key.Equals(relevantKey))
          {
            allCurrentValues.Add(propertyId, newValue.Value);
            break;
          }
        }
      }
    }
    return allCurrentValues;
  }

  public object GetOldValue(ITxRoboticLocationOperation location, string propertyId)
  {
    object oldValue = (object) null;
    LocationPropertyIdKey key = new LocationPropertyIdKey(location, propertyId);
    if (this.AreListsValid() && this._oldValues.ContainsKey(key))
      oldValue = this._oldValues[key];
    return oldValue;
  }

  public object GetNewValue(ITxRoboticLocationOperation location, string propertyId)
  {
    object newValue = (object) null;
    LocationPropertyIdKey key = new LocationPropertyIdKey(location, propertyId);
    if (this.AreListsValid() && this._newValues.ContainsKey(key))
      newValue = this._oldValues[key];
    return newValue;
  }

  private bool AreListsValid()
  {
    return this._oldValues != null && this._newValues != null && this._propertyIds != null && this._newValues.Count == this._oldValues.Count;
  }

  private LocationPropertyIdKey GetRelevantKey(
    Dictionary<LocationPropertyIdKey, object> valuesList,
    ITxRoboticLocationOperation location,
    string propertyId)
  {
    foreach (KeyValuePair<LocationPropertyIdKey, object> values in valuesList)
    {
      if (values.Key.Equals(location, propertyId))
        return values.Key;
    }
    return (LocationPropertyIdKey) null;
  }

  public void Clear()
  {
    this._newValues.Clear();
    this._oldValues.Clear();
    this._propertyIds.Clear();
  }

  public bool HasOldValue(ITxRoboticLocationOperation location, string propertyId)
  {
    if (this._oldValues == null)
      return false;
    foreach (KeyValuePair<LocationPropertyIdKey, object> oldValue in this._oldValues)
    {
      if (oldValue.Key.Equals(location, propertyId))
        return oldValue.Value != null;
    }
    return false;
  }
}
